using System;
using System.Collections;
using HCP.Responses;
using HCP.SimpleJSON;
using UnityEngine;

namespace HCP.Requests
{
	public class ChangeScene : JobRequest
	{
		public ChangeScene(JSONClass json) : base(json)
		{
		}

		public string SceneName
		{
			get
			{
				return base.Data["scene"];
			}
		}

		public string UnityScene
		{
			get
			{
				return base.Data["unityScene"];
			}
		}

		public object Param
		{
			get
			{
				return base.Data["param"];
			}
		}

		public override JobResponse Process()
		{
			TaskManager.StartCoroutine(this.CoCloseAllPopupThenChangeScene());
			return new StringResponse();
		}

		private IEnumerator CoCloseAllPopupThenChangeScene()
		{
			GameObject popup = Scene.CurrentPopup();
			while (popup != null)
			{
				Scene.ClosePopup(popup.name, null);
				popup = Scene.CurrentPopup();
				yield return new WaitForSeconds(0.3f);
			}
			if (!string.IsNullOrEmpty(this.UnityScene))
			{
				Scene.ChangeUnityScene(this.UnityScene, this.SceneName, this.Param, "Loading");
			}
			else if (this.SceneName.EndsWith("Popup"))
			{
				Scene.ShowPopup(this.SceneName, this.Param, null);
			}
			else
			{
				Scene.ChangeTo(this.SceneName, this.Param);
			}
			yield break;
		}
	}
}
